﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class MedFormDetails : Form
    {
        private MedForm _parent;
        public DataSets.Dictionary.MedFormDataTable MedFormData
        {
            get
            {
                return _parent.dictionary.MedForm;
            }
        }
        private Int32 _medFormId;

        public Int32 MedFormID
        {
            get { return _medFormId; }
            set { _medFormId = value; }
        }
        private Util.ActionType _openAction;

        public Util.ActionType OpenAction
        {
            get { return _openAction; }
            set { _openAction = value; }
        }
        private DataSets.Dictionary.MedFormRow _curRow;

        public DataSets.Dictionary.MedFormRow CurrentRow
        {
            get { return _curRow; }
            set { _curRow = value; }
        }

        private MedFormDetails()
        {
            InitializeComponent();
        }
        public MedFormDetails(Util.ActionType actType, Int32 medFormId, MedForm parent)
        {
            InitializeComponent();
            OpenAction = actType;
            MedFormID = medFormId;
            _parent = parent;
        }

        private void MedFormDetails_Load(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }

        }
        private void InitInsert()
        {
            btnDelete.Visible = false;
            btnInsertUpdate.Text = "Сохранить";
        }
        private void InitUpdate()
        {
            btnDelete.Visible = true;
            btnInsertUpdate.Text = "Изменить";
            CurrentRow = MedFormData.FindById(this.MedFormID);
            txbName.Text = CurrentRow.Name;
            txbNote.Text = CurrentRow.Note;
            txbShortName.Text = CurrentRow.ShortName;
        }
        private void MedFormInsert()
        {
            DataSets.DictionaryTableAdapters.MedFormTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.MedFormTableAdapter();
            DataSets.Dictionary.MedFormRow mfr = MedFormData.NewMedFormRow();
            mfr.Name = txbName.Text;
            mfr.ShortName = txbShortName.Text;
            mfr.Note = txbNote.Text;
            mfr.MD = DateTime.Now;
            mfr.MW = FirstForm.Login;
            mfr.ToDelete = false;
            MedFormData.Rows.Add(mfr);
            adapter.Update(MedFormData);
            mfr.OriginalMD = mfr.MD;            
            MedFormData.AcceptChanges();
            
        }
        private void MedFormUpdate()
        {
            DataSets.DictionaryTableAdapters.MedFormTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.MedFormTableAdapter();
           
            CurrentRow.Name = txbName.Text;
            CurrentRow.ShortName = txbShortName.Text;
            CurrentRow.Note = txbNote.Text;
            CurrentRow.MD = DateTime.Now;
            CurrentRow.MW = FirstForm.Login;
            CurrentRow.ToDelete = false;
            adapter.Update(MedFormData);
            CurrentRow.OriginalMD = CurrentRow.MD;
            MedFormData.AcceptChanges();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsertUpdate_Click(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    this.MedFormInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    this.MedFormUpdate();
                    break;
                default:
                    break;
            }
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите удалить медицинскую форму " + CurrentRow.Name + "?", "Удаление медицинской формы", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataSets.DictionaryTableAdapters.MedFormTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.MedFormTableAdapter();
                if (DIProject.Data.Sync.IsAdmin)
                {
                    CurrentRow.MW = Properties.Resources.strPhysicallyDelete;
                }
                else
                {
                    CurrentRow.MW = FirstForm.Login;
                }
                CurrentRow.Delete();
                adapter.Update(MedFormData);
                MedFormData.AcceptChanges();                
            }
            this.Close();
        }

        private void txbName_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                if (string.IsNullOrEmpty(tb.Text))
                {
                    MessageBox.Show("Значение поля \"Наименование\" не может быть пустым", "Введите значение");
                    e.Cancel = true;
                    tb.Focus();
                }
            }
        }

        private void txbShortName_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                if (string.IsNullOrEmpty(tb.Text))
                {
                    MessageBox.Show("Значение поля \"Короткое наименование\" не может быть пустым", "Введите значение");
                    e.Cancel = true;
                    tb.Focus();
                }
            }
        }
    }
}